﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Frame
{
    public class StringControl
    {
        public static string ArrayToString(List<int> list)
        {
            string str = "";
            if (list.Count == 0)
                return str;
            for (int i = 0; i < list.Count - 1; i++)
            {
                str += list[i].ToString() + ",";
            }
            str += list[list.Count - 1].ToString();
            return str;
        }

        public static string ByteToHexStr(byte[] bytes)
        {
            string returnStr = "";
            if (bytes != null)
            {
                for (int i = 0; i < bytes.Length; i++)
                {
                    returnStr += bytes[i].ToString("X2") + "";              
                }
            }
            return returnStr;
        }

        public static byte[] StrTOHexByte(string hexString)
        {
            //Debug.Log(hexString);
            hexString = hexString.Replace(" ", "");
            if (hexString == "" && hexString.Length < 2)
                return null;            
            if ((hexString.Length % 2) != 0)
                hexString += " ";
            byte[] returnBytes = new byte[hexString.Length / 2];
            int len = hexString.Length;
            //Debug.Log(len);
            for (int i = 0; i < returnBytes.Length; i++)
            {
                //Debug.Log(returnBytes);
                //Debug.Log(hexString);
                if (len >= 2 )
                {
                    //Debug.Log(returnBytes.Length);
                    //Debug.Log(i);
                    returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
                    //Debug.Log(returnBytes[i]);
                    len -= 2;
                    //Debug.Log(len);
                }
            }
            return returnBytes;
        }

        public static int GetNumBit(int num, int bit = 0)
        {
            //Debug.Log(num);
            num = num >> bit;
            //Debug.Log(num);
            int retn = num & 1;
            //Debug.Log(retn);
            return retn;
        }

        public IList<char> HexSet = new List<char>()
        { '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','a','b','c','d','e','f' };

        public static string IsHexadecimal(string hex)
        {
            foreach (char item in hex)
            {
                if (!hex.Contains<char>(item))
                    return "";
            }
            return hex;

        }


        public static string insertStr(string value, int interval, string s)
        {
            int count = value.Length / interval;
            for (int i = 0; i < count-1; i++)
            {
                int insertPos = (i + 1) * interval + i * 1;
                //Debug.Log(insertPos);
                value = value.Insert(insertPos, s);
                //Debug.Log(value);
            }
            return value;
        }

        public static string AddZero(string value, int NumberLen=2)
        {
            int len = value.Length;
            if(len> NumberLen)
            {
                Debug.Log(value);
                value = value.Substring(len-2, NumberLen);
                Debug.Log(value);
            }
            for (int i = 0; i < (NumberLen - len); i++)
            {
                value = "0" + value;
            }
            return value;
        }

        public static string HexToDec(string Hex)
        {
            int a = Convert.ToInt32(Hex, 16);
            return AddZero(a.ToString(), 3);
        }

        public static string DecToHex(string Dec)
        {
            int a = Convert.ToInt32(Dec);
            return AddZero(a.ToString("X2"));
        }

        //查找出列表中的所有重复元素
        public static List<string> QueryRepeatElementOfList(List<string> list)
        {
            List<string> listTmp = new List<string>();
            if (list != null && list.Count > 0)
            {
                listTmp = list.GroupBy(x => x)
                              .Where(g => g.Count() > 1)
                              .Select(y => y.Key)
                              .ToList();
            }
            return listTmp;
        }

        //查找出列表中的所有重复元素
        public static List<int> QueryRepeatElementOfList(List<int> list)
        {
            List<int> listTmp = new List<int>();
            if (list != null && list.Count > 0)
            {
                listTmp = list.GroupBy(x => x)
                              .Where(g => g.Count() > 1)
                              .Select(y => y.Key)
                              .ToList();
            }
            return listTmp;
        }

        //查找出列表中的所有重复元素
        public static List<double> QueryRepeatElementOfList(List<double> list)
        {
            List<double> listTmp = new List<double>();
            if (list != null && list.Count > 0)
            {
                listTmp = list.GroupBy(x => x)
                              .Where(g => g.Count() > 1)
                              .Select(y => y.Key)
                              .ToList();
            }
            return listTmp;
        }

        //查找出列表中的所有重复元素及其重复次数
        public static Dictionary<string, int> QueryRepeatElementAndCountOfList(List<string> list)
        {
            Dictionary<string, int> DicTmp = new Dictionary<string, int>();
            if (list != null && list.Count > 0)
            {
                DicTmp = list.GroupBy(x => x)
                             .Where(g => g.Count() > 1)
                             .ToDictionary(x => x.Key, y => y.Count());
            }
            return DicTmp;
        }

        /// <summary>
        /// 获取字符串中的数字
        /// </summary>
        /// <param name="str">输入字符串</param>
        /// <returns></returns>
        public static int GetNumFormString(string str)
        {
            // 使用正则表达式匹配数字的模式
            string pattern = @"\d+";

            // 创建正则表达式对象
            Regex regex = new Regex(pattern);

            // 在字符串中查找所有匹配的数字
            MatchCollection matches = regex.Matches(str);

            //// 遍历匹配结果并输出
            //foreach (Match match in matches)
            //{
                
            //}
            return Convert.ToInt32(matches[0].Value);
        }
        /// <summary>
        /// 只保留中文字符
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string KeepChinese(string input)
        {
            Regex regex = new Regex(@"[\u4e00-\u9fa5]+"); // 匹配中文字符的正则表达式
            MatchCollection matches = regex.Matches(input); // 提取字符串中的中文字符
            string result = string.Empty;
            foreach (Match match in matches)
            {
                result += match.Value;
            }
            return result;
        }
        /// <summary>
        /// 只保留数字
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string KeepNumbers(string input)
        {
            Regex regex = new Regex(@"\d+"); // 匹配数字的正则表达式
            MatchCollection matches = regex.Matches(input); // 提取字符串中的数字
            string result = string.Empty;
            foreach (Match match in matches)
            {
                result += match.Value;
            }
            return result;
        }
        /// <summary>
        /// 只保留英文
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string KeepEnglish(string input)
        {
            string result = Regex.Replace(input, "[^a-zA-Z]", "");
            //string result = new string(input.Where(c => Char.IsLetter(c)).ToArray());
            return result;
        }
        /// <summary>
        /// 替换字符
        /// </summary>
        /// <param name="input">输入字符</param>
        /// <param name="sorce">被替换的字符</param>
        /// <param name="tartget">要替换的字符</param>
        /// <returns></returns>
        public static string RespaceName(string input, string sorce, string tartget)
        {
            if(input == sorce)
            {
                string result = input.Replace(sorce, tartget); // 将字符串中的所有 "o" 替换为 "x"
                return result;
            }
            return input;
        }

    }
   
}
